<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\helpers\traits\ImageUpload;
use app\helpers\traits\SearchDataForModel;
use app\model\TargetFile;
use think\Request;

class Target extends BaseController
{
    use SearchDataForModel, ImageUpload;

    /**
     * @User: 刘海龙
     * @Date: 2021/8/3
     * @Time: 9:53
     * @return \think\response\Json
     * 上传文件
     */
    public function uploadTarget()
    {
        if (!$this->request->file('file')) {
            return $this->response(501, '请选择上传文件');
        }
        $file = $this->request->file('file');
        $result = $this->save($file, 'target');
        return $this->response($result);
    }

    /**
     * @User: 刘海龙
     * @Date: 2021/8/3
     * @Time: 10:14
     * @return \think\response\Json
     * 保存文件
     */
    public function saveFile()
    {
        $data = $this->request->param();
        TargetFile::create($data);
        return $this->response();
    }

    /**
     * @User: 刘海龙
     * @Date: 2021/8/3
     * @Time: 10:17
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 数据列表
     */
    public function getTargetList(){
        $limit = $this->request->param('limit',10);
        $res = $this->search(new TargetFile(),[],$limit);
        return $this->response($res);
    }

    /**
     * @param $id
     * @User: 刘海龙
     * @Date: 2021/8/4
     * @Time: 14:14
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 删除
     */
    public function DelTarget($id){
        $ids = explode(',', $id);
        foreach ($ids as $id) {
            $img = TargetFile::find($id);
            if (is_null($img)) {
                return $this->response(404, '此文件不存在');
            }
            $img->delete();
        }
        return $this->response();
    }
}
